package org.dev.lms.controller;

import org.dev.lms.entity.Book;
import org.dev.lms.entity.LibraryBranch;
import org.dev.lms.service.BookInventoryService;
import org.dev.lms.entity.BookInventory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dev.common.core.page.PaginAtion;
import org.dev.common.core.result.ResponseResult;
import org.dev.lms.service.BookService;
import org.dev.lms.service.LibraryBranchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.dev.common.core.aop.OperLog;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/bookInventory")
public class BookInventoryController {
    @Autowired
    public BookInventoryService bookInventoryService;

    @Autowired
    BookService bookService;

    @Autowired
    LibraryBranchService libraryBranchService;

    /**
     * 保存和修改公用的
     *
     * @param bookInventory 传递的实体
     * @return ResponseResult转换结果
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperLog(description = "bookInventory保存和修改")
    public ResponseResult<String> save(@RequestBody BookInventory bookInventory) {
        boolean result = false;
        try {
            if (bookInventory.getId() != null) {
                result = bookInventoryService.updateById(bookInventory);
            } else {
                result = bookInventoryService.save(bookInventory);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        if (result)
            return ResponseResult.success();
        return ResponseResult.error();
    }

    /**
     * 获取对象信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @OperLog(description = "bookInventory获取对象信息")
    public ResponseResult<BookInventory> get(@RequestParam("id") Long id) {
        BookInventory bookInventory = bookInventoryService.getById(id);
        return ResponseResult.success(bookInventory);
    }

    /**
     * 删除对象信息
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @OperLog(description = "bookInventory删除对象信息")
    public ResponseResult<String> delete(@RequestBody List<Long> ids) {
        try {
            bookInventoryService.removeByIds(ids);
            return ResponseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 分页查询数据信息
     *
     * @param bookInventory
     * @return
     */
    @RequestMapping("/list")
    @OperLog(description = "bookInventory分页查询数据信息")
    public ResponseResult<IPage<BookInventory>> list(BookInventory bookInventory, PaginAtion pagination) {
        Page page = pagination.getPage();
        IPage<BookInventory> inventoryIPage = bookInventoryService.page(page, new QueryWrapper<BookInventory>(bookInventory));
        List<BookInventory> inventoryList = inventoryIPage.getRecords();
        if (inventoryList != null && inventoryList.size() > 0) {
            List<Long> bookIds = inventoryList.stream().map(BookInventory::getBookId).distinct().collect(Collectors.toList());
            QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", bookIds);
            List<Book> bookList = this.bookService.list(queryWrapper);
            if (bookList != null && bookList.size() > 0) {
                inventoryList.stream().forEach(item -> {
                    bookList.forEach(book -> {
                        if (item.getBookId().equals(book.getId())) {
                            item.setBookBarCode(book.getBookBarCode());
                            item.setBookName(book.getBookName());
                            item.setAuthor(book.getAuthor());
                            item.setPublisher(book.getPublisher());
                        }
                    });
                });
            }
            //

            List<Long> libraryIds = inventoryList.stream().map(BookInventory::getLibraryId).distinct().collect(Collectors.toList());
            QueryWrapper<LibraryBranch> libraryBranchQueryWrapper = new QueryWrapper<>();
            libraryBranchQueryWrapper.in("id", libraryIds);
            List<LibraryBranch> libraryBranchList = this.libraryBranchService.list(libraryBranchQueryWrapper);
            if (libraryBranchList != null && libraryBranchList.size() > 0) {
                libraryBranchList.forEach(libraryBranch -> {
                    inventoryList.stream().forEach(item -> {
                        if (libraryBranch.getId().equals(item.getLibraryId())) {
                            item.setLibraryName(libraryBranch.getLibraryName());
                        }
                    });
                });
            }
        }

        return ResponseResult.success(inventoryIPage);
    }
}